unit uBancos;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls, Vcl.Mask,
  Vcl.DBCtrls, Vcl.Buttons, JvDBControls, Vcl.Grids, Vcl.DBGrids, JvExStdCtrls,
  JvButton, JvCtrls, Vcl.ExtCtrls, Vcl.ComCtrls;

type
  TfrmBancos = class(TForm)
    PageControlBan: TPageControl;
    TabSheetConsulta: TTabSheet;
    Panel1: TPanel;
    btnNovo: TJvImgBtn;
    btnEditar: TJvImgBtn;
    btnExcluir: TJvImgBtn;
    JvImgBtn1: TJvImgBtn;
    dbGridban: TDBGrid;
    JvDBNavigator1: TJvDBNavigator;
    TabSheetCad: TTabSheet;
    Panel2: TPanel;
    btnOk: TBitBtn;
    btnCancelar: TBitBtn;
    GroupBox1: TGroupBox;
    dbEdtBandescr: TDBEdit;
    dsTblban: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure btnNovoClick(Sender: TObject);
    procedure btnEditarClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure JvImgBtn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnOkClick(Sender: TObject);
    procedure btnCancelarClick(Sender: TObject);
  private
    { Private declarations }
    procedure ControlarPageControl(APageIndex: integer);
  public
    { Public declarations }
  end;

var
  frmBancos: TfrmBancos;

implementation

{$R *.dfm}

uses uDadosContasPagar;

{ TfrmBancos }

procedure TfrmBancos.btnCancelarClick(Sender: TObject);
begin
  dmDadosContasPagar.CalcelarBanco;

  ControlarPageControl(0);
end;

procedure TfrmBancos.btnEditarClick(Sender: TObject);
begin
  dmDadosContasPagar.EditarBanco;

  ControlarPageControl(1);
end;

procedure TfrmBancos.btnExcluirClick(Sender: TObject);
begin
  try
    dmDadosContasPagar.ExcluirBanco;

    dmDadosContasPagar.FiltrarBancos;
  except
    on E: Exception do
    begin
      MessageDlg(e.Message, mtError,[mbOK],0);

      dmDadosContasPagar.FiltrarBancos;
    end;
  end;
end;

procedure TfrmBancos.btnNovoClick(Sender: TObject);
begin
  dmDadosContasPagar.AdicionarBanco;

  ControlarPageControl(1);
end;

procedure TfrmBancos.btnOkClick(Sender: TObject);
begin
  dmDadosContasPagar.SalvarBanco;

  dmDadosContasPagar.FiltrarBancos;

  ControlarPageControl(0);
end;

procedure TfrmBancos.ControlarPageControl(APageIndex: integer);
begin
  if APageIndex = 0 then
  begin
    PageControlBan.Pages[0].TabVisible := true;  // Tab de consulta
    PageControlBan.Pages[1].TabVisible := false; // Tab de cadastro
    PageControlBan.ActivePageIndex := 0;

    try
      dbGridban.SetFocus;
    except
    end;
  end
  else
  begin
    PageControlBan.Pages[0].TabVisible := false; // Tab de consulta
    PageControlBan.Pages[1].TabVisible := true;  // Tab de cadastro
    PageControlBan.ActivePageIndex := 1;

    try
      dbEdtBandescr.SetFocus;
    except
    end;
  end;
end;

procedure TfrmBancos.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  dmDadosContasPagar.CalcelarBanco;
end;

procedure TfrmBancos.FormCreate(Sender: TObject);
begin
  ControlarPageControl(0);

  dmDadosContasPagar.FiltrarBancos;
end;

procedure TfrmBancos.JvImgBtn1Click(Sender: TObject);
begin
  close;
end;

end.
